home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / prbgi00c.zip / BGIDEMO.C next >
C/C++ Source or Header  |  1992-03-20  |  50KB  |  1,666 lines

  1. /* This program is not mine.
  2.    It was included with Turbo/Borland C(++)
  3.    and I only modified it a little  to demonstrate
  4.    features of my PRINTBGI library.
  5.    It is included here for demonstration only
  6.    and cannot be used for any other purposes. ( Could it? ).
  7.  
  8.    Original Copyright notice follows.
  9.  */
  10.  
  11. /*
  12.    GRAPHICS DEMO FOR TURBO C 2.0
  13.  
  14.    Copyright (c) 1987,88,90 Borland International. All rights reserved.
  15.  
  16.    From the command line, use:
  17.  
  18.       tcc bgidemo graphics.lib
  19.  
  20. */
  21.  
  22. char  BGI_path [80] = "d:\\bc\\bgi";
  23.  
  24. #ifdef __TINY__
  25. #error BGIDEMO will not run in the tiny model.
  26. #endif
  27.  
  28. #include <dos.h>
  29. #include <math.h>
  30. #include <conio.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <stdarg.h>
  34. #include <string.h>
  35. #include <graphics.h>
  36.  
  37. #include <stdarg.h>
  38. #include "prtgraph.h"
  39.  
  40. #define min(a,b)    (((a) < (b)) ? (a) : (b))
  41.  
  42. #define ESC 0x1b        /* Define the escape key   */
  43. #define TRUE   1        /* Define some handy constants   */
  44. #define FALSE  0        /* Define some handy constants   */
  45. #define PI  3.14159     /* Define a value for PI   */
  46. #define ON  1        /* Define some handy constants   */
  47. #define OFF 0        /* Define some handy constants   */
  48.  
  49. char *Fonts[] = {
  50.   "DefaultFont",   "TriplexFont",   "SmallFont",
  51.   "SansSerifFont", "GothicFont"
  52. };
  53.  
  54. char *LineStyles[] = {
  55.   "SolidLn",  "DottedLn",  "CenterLn",  "DashedLn",  "UserBitLn"
  56. };
  57.  
  58. char *FillStyles[] = {
  59.   "EmptyFill",  "SolidFill",      "LineFill",      "LtSlashFill",
  60.   "SlashFill",  "BkSlashFill",    "LtBkSlashFill", "HatchFill",
  61.   "XHatchFill", "InterleaveFill", "WideDotFill",   "CloseDotFill"
  62. };
  63.  
  64. char *TextDirect[] = {
  65.   "HorizDir",  "VertDir"
  66. };
  67.  
  68. char *HorizJust[] = {
  69.   "LeftText",   "CenterText",   "RightText"
  70. };
  71.  
  72. char *VertJust[] = {
  73.   "BottomText",  "CenterText",  "TopText"
  74. };
  75.  
  76. struct PTS {
  77.   int x, y;
  78. }; /* Structure to hold vertex points  */
  79.  
  80. int    GraphDriver;     /* The Graphics device driver    */
  81. int    GraphMode;    /* The Graphics mode value    */
  82. int   xasp_,yasp_;
  83. #define  AspectRatio ( getaspectratio(&xasp_,&yasp_),(double)xasp_/(double)yasp_ )
  84. /* double AspectRatio;  */    /* Aspect ratio of a pixel on the screen*/
  85. #define  MaxX  (getmaxx())
  86. #define  MaxY  (getmaxy())
  87. /* int    MaxX, MaxY;   */ /* The maximum resolution of the screen */
  88. #define  MaxColors (getmaxcolor()+1)
  89. /* int    MaxColors; */    /* The maximum # of colors available   */
  90. int    ErrorCode;    /* Reports any graphics errors      */
  91. struct palettetype palette;      /* Used to read palette info  */
  92.  
  93. /*                         */
  94. /* Function prototypes                 */
  95. /*                         */
  96.  
  97. void Initialize(void);
  98. int ReportStatus(void);
  99. int TextDump(void);
  100. int Bar3DDemo(void);
  101. int RandomBars(void);
  102. int TextDemo(void);
  103. int ColorDemo(void);
  104. int ArcDemo(void);
  105. int CircleDemo(void);
  106. int PieDemo(void);
  107. int BarDemo(void);
  108. int LineRelDemo(void);
  109. int PutPixelDemo(void);
  110. int PutImageDemo(void);
  111. int LineToDemo(void);
  112. int LineStyleDemo(void);
  113. int CRTModeDemo(void);
  114. int UserLineStyleDemo(void);
  115. int FillStyleDemo(void);
  116. int FillPatternDemo(void);
  117. int PaletteDemo(void);
  118. int PolyDemo(void);
  119. int SayGoodbye(void);
  120. void Pause(void);
  121. void MainWindow(char *header);
  122. void StatusLine(char *msg);
  123. void DrawBorder(void);
  124. void changetextstyle(int font, int direction, int charsize);
  125. int  gprintf(int *xloc, int *yloc, char *fmt, ... );
  126.  
  127.  
  128. static int  picwidth=4000,picheight=3000,leftmargin=0,topmargin=0,
  129.             PicRotate=0, PicInverse=1;
  130. static unsigned   PRTno;
  131. static int  mode;
  132. static char OutName[64]="PRN";
  133.  
  134. int _scanf( const char * format, ... )
  135. {  va_list ap;
  136.    int     c,r;
  137.  
  138.    c=getchar();
  139.    if ( c=='\n' )  return 0;
  140.    va_start(ap,format);
  141.    ungetc(c,stdin);
  142.    r = vscanf (format,ap);  (void)getchar();
  143.    va_end(ap);
  144.    return r;
  145. }
  146.  
  147.  
  148. /*-----------------------*/
  149. void AskOfParameters(void)
  150. /*-----------------------*/
  151. {  int   c,MAXmode;
  152.    char  *modename;
  153.  
  154.    cputs ( "\r\n Choose printer mode operation\r\n" );
  155.    PRT_MaxMode ( PRTno, &MAXmode );
  156.    for ( mode=0; mode<=MAXmode; mode++ )
  157.    {
  158.       PRT_ModeName(PRTno,mode,&modename );
  159.       cprintf ( "        %2d - %s\r\n", mode, modename );
  160.    }
  161.    mode=MAXmode+1;
  162.    do
  163.    {
  164.      c=getch();
  165.      if (c==0) getch();
  166.      else if ((unsigned)c-'0'<=(unsigned)MAXmode) mode=c-'0';
  167.    }
  168.    while ( mode>MAXmode);
  169.  
  170.    cprintf ( "\n\r Picture width in 1/1000 inch [%d] ", picwidth );
  171.    _scanf ( "%d", &picwidth );
  172.    cprintf ( " Picture height in 1/1000 inch [%d] ", picheight );
  173.    _scanf ( "%d", &picheight );
  174.  
  175.    cprintf ( " Top margin in 1/1000 inch [%d] ", topmargin );
  176.    _scanf ( "%d", &topmargin );
  177.    cprintf ( " Left margin in 1/1000 inch [%d] ", leftmargin );
  178.    _scanf ( "%d", &leftmargin );
  179.    cprintf ( " Rotate picture [%d] ", PicRotate );
  180.    _scanf ( "%d", &PicRotate );
  181.    cprintf ( " Inverse picture [%d] ", PicInverse );
  182.    _scanf ( "%d", &PicInverse );
  183.  
  184. }
  185.  
  186.  
  187. int static  printing=0,asking=0;
  188. int PRT_drv;
  189. /*---------------------------------*/
  190. void DrawAndPrint ( int func(void) )
  191. /*---------------------------------*/
  192. {  int   rc;
  193.    int   PRT_mode=0;
  194.  
  195.    do
  196.    {
  197.       asking=0;
  198.       func();
  199.       if ( asking )
  200.       {  int  mode;
  201.          mode = getgraphmode();
  202.          restorecrtmode();
  203.          AskOfParameters();
  204.          setgraphmode( mode );
  205.       }
  206.    }
  207.    while ( asking );
  208.  
  209.    if ( printing )   /* Have user pressed Ctrl-P ? */
  210.    {
  211.       PRT_SetDriver ( PRTno, mode,picwidth,picheight,
  212.                       ( PicRotate ? PRT_ROTATE:0 ) | ( PicInverse ? PRT_INVERSE : 0 ) );
  213.       PRT_SetMargins ( leftmargin, topmargin );
  214.       PRT_Buffer ( 0L, 0, 1 );
  215.       rc=PRT_PrintBGI ( &PRT_drv, &PRT_mode, BGI_path, func );
  216.       if ( rc )
  217.       {  int  mode;
  218.          mode=getgraphmode();
  219.          restorecrtmode();
  220.          cprintf ("\r\n error code %d (%s) from PRT_PrintBGI \r\n", rc,
  221.                                  PRT_errormsg(rc) );
  222.          getch();
  223.          setgraphmode(mode);
  224.       }
  225.       printing=0;
  226.       if (PRT_DriverNo()>=HPLJII)     /* form feed for laser printers */
  227.       {  int   mode,handle;
  228.          mode = getgraphmode();
  229.          restorecrtmode();
  230.          handle = PRT_Open(OutName);
  231.          PRT_Write ( handle,"\x0c",1 ); /*Form Feed*/
  232.          PRT_Close(handle);
  233.          setgraphmode(mode);
  234.          rc=graphresult();
  235.       }
  236.    }
  237. }
  238.  
  239. /*-----------------*/
  240. void  InitPBGI(void)
  241. /*-----------------*/
  242. {
  243.    char  *PRTname;
  244.    char  s[64];
  245.    unsigned MaxPrinterNo;
  246.    int      rc;
  247.  
  248.    MaxPrinterNo = PRT_MaxDriver();
  249.    clrscr();
  250.    cputs ( "\r\n\n\n"
  251.            "\n\r This is a sample program (developed from Borland's BGIDEMO.C)"
  252.            "\n\r demonstrating some of the features of PrintBGI toolkit"
  253.            "\n\r Hope you'll find it usefull (the whole package not this program,"
  254.            "\n\r of course).\n\r"
  255.            "\n\r Since this is only an example program for demonstrating purposes"
  256.            "\n\r it uses standard C scanf function to input data with all"
  257.            "\n\r consequences of it." );
  258.    cputs ( "\n\r \n\r                   Press any key to continue");
  259.    if ( getch()==0 ) getch();
  260.    clrscr();
  261.  
  262.    cputs ( "\r\n    Choose printer type\r\n" );
  263.    for ( PRTno=0; PRTno<=MaxPrinterNo; PRTno++ )
  264.    {
  265.       PRT_DriverName(PRTno,&PRTname );
  266.       cprintf ( "        %2d - %s or compatible \r\n", PRTno, PRTname );
  267.    }
  268.    do
  269.    {
  270.       scanf( "%d",&PRTno);
  271.       (void)getchar();
  272.    }
  273.    while ( PRTno>MaxPrinterNo );
  274.  
  275.    clrscr();
  276.    cprintf ( "\n\r Output device name [%s]", OutName );
  277.    gets ( s ); if (*s!=0) strcpy(OutName,s);
  278.  
  279.    clrscr();
  280.    PRT_drv = PRT_installuserdriver ( "BitImage", NULL );
  281.    rc = PRT_registerfarbgidriver ( BitImage );
  282.  
  283.    AskOfParameters();
  284.    cputs ( "\r\n\r\n You will be able to change above parameters by pressing Ctrl-C." );
  285.    cputs ( "\n\r \n\r                   Press any key to continue");
  286.    if ( getch()==0 ) getch();
  287.  
  288.    PRT_SetOutName ( OutName );
  289. }
  290.  
  291. /*-------------------*/
  292. void grErrorTest(void)
  293. /*-------------------*/
  294. { int ErrorCode;
  295.   ErrorCode = graphresult();     /* Read result of initialization*/
  296.   if( ErrorCode == grOk ) return;
  297.   /* Error occurred  */
  298.   closegraph();
  299.   printf(" Graphics System Error: %s\n", grapherrormsg( ErrorCode ) );
  300.   exit( 16 );
  301. }
  302.  
  303. /*                         */
  304. /* Begin main function                 */
  305. /*                         */
  306.  
  307. int main()
  308. {
  309.  
  310.    InitPBGI();
  311.   Initialize();      /* Set system into Graphics mode */
  312.  
  313.   DrawAndPrint ( ReportStatus );    /* Report results of the initialization */
  314.   DrawAndPrint ( ColorDemo );       /* Begin actual demonstration    */
  315.   if( GraphDriver==EGA || GraphDriver==EGALO || GraphDriver==VGA )
  316.     PaletteDemo();
  317.   DrawAndPrint ( PutPixelDemo );
  318.   DrawAndPrint ( PutImageDemo );
  319.   DrawAndPrint ( Bar3DDemo );
  320.   DrawAndPrint ( BarDemo );
  321.   DrawAndPrint ( RandomBars );
  322.   DrawAndPrint ( ArcDemo );
  323.   DrawAndPrint ( CircleDemo );
  324.   DrawAndPrint ( PieDemo );
  325.   DrawAndPrint ( LineRelDemo );
  326.   DrawAndPrint ( LineToDemo );
  327.   DrawAndPrint ( LineStyleDemo );
  328.   DrawAndPrint ( UserLineStyleDemo );
  329.   TextDump();
  330.   TextDemo();
  331.   /* CRTModeDemo(); */
  332.   DrawAndPrint ( FillStyleDemo );
  333.   DrawAndPrint ( FillPatternDemo);
  334.   DrawAndPrint ( PolyDemo );
  335.  
  336.   DrawAndPrint ( SayGoodbye );      /* Give user the closing screen  */
  337.  
  338.   closegraph();      /* Return the system to text mode   */
  339.   return(0);
  340. }
  341.  
  342. /*                         */
  343. /* INITIALIZE: Initializes the graphics system and reports  */
  344. /* any errors which occured.              */
  345. /*                         */
  346.  
  347. void Initialize(void)
  348. {
  349.   /* int xasp, yasp;  */      /* Used to read the aspect ratio*/
  350.  
  351.   GraphDriver = DETECT;       /* Request auto-detection  */
  352.   initgraph( &GraphDriver, &GraphMode, BGI_path );
  353.   grErrorTest();
  354.  
  355.   getpalette( &palette );     /* Read the palette from board   */
  356.   /*  MaxColors = getmaxcolor() + 1;   */ /* Read maximum number of colors*/
  357.  
  358.   /*  MaxX = getmaxx(); */
  359.   /*  MaxY = getmaxy(); */    /* Read size of screen     */
  360.  
  361.   /* getaspectratio( &xasp, &yasp ); */   /* read the hardware aspect   */
  362.   /* AspectRatio = (double)xasp / (double)yasp; */ /* Get correction factor   */
  363.  
  364. }
  365.  
  366. /*                         */
  367. /* REPORTSTATUS: Report the current configuration of the system   */
  368. /* after the auto-detect initialization.           */
  369. /*                         */
  370.  
  371. int ReportStatus(void)
  372. {
  373.   struct viewporttype     viewinfo; /* Params for inquiry procedures*/
  374.   struct linesettingstype lineinfo;
  375.   struct fillsettingstype fillinfo;
  376.   struct textsettingstype textinfo;
  377.   struct palettetype   palette;
  378.  
  379.   char *driver, *mode;        /* Strings for driver and mode   */
  380.   int x, y;
  381.  
  382.   getviewsettings( &viewinfo );
  383.   getlinesettings( &lineinfo );
  384.   getfillsettings( &fillinfo );
  385.   gettextsettings( &textinfo );
  386.   getpalette( &palette );
  387.  
  388.   x = 10;
  389.   y = 4;
  390.  
  391.   MainWindow( "Status report after InitGraph" );
  392.   settextjustify( LEFT_TEXT, TOP_TEXT );
  393.  
  394.   driver = getdrivername();
  395.   mode = getmodename(GraphMode); /* get current setting     */
  396.  
  397.   gprintf( &x, &y, "Graphics device    : %-20s (%d)", driver, GraphDriver );
  398.   gprintf( &x, &y, "Graphics mode      : %-20s (%d)", mode, GraphMode );
  399.   gprintf( &x, &y, "Screen resolution  : ( 0, 0, %d, %d )", getmaxx(), getmaxy() );
  400.  
  401.   gprintf( &x, &y, "Current view port  : ( %d, %d, %d, %d )",
  402.   viewinfo.left, viewinfo.top, viewinfo.right, viewinfo.bottom );
  403.   gprintf( &x, &y, "Clipping           : %s", viewinfo.clip ? "ON" : "OFF" );
  404.  
  405.   gprintf( &x, &y, "Current position   : ( %d, %d )", getx(), gety() );
  406.   gprintf( &x, &y, "Colors available   : %d", MaxColors );
  407.   gprintf( &x, &y, "Current color      : %d", getcolor() );
  408.  
  409.   gprintf( &x, &y, "Line style         : %s", LineStyles[ lineinfo.linestyle ] );
  410.   gprintf( &x, &y, "Line thickness     : %d", lineinfo.thickness );
  411.  
  412.   gprintf( &x, &y, "Current fill style : %s", FillStyles[ fillinfo.pattern ] );
  413.   gprintf( &x, &y, "Current fill color : %d", fillinfo.color );
  414.  
  415.   gprintf( &x, &y, "Current font       : %s", Fonts[ textinfo.font ] );
  416.   gprintf( &x, &y, "Text direction     : %s", TextDirect[ textinfo.direction ] );
  417.   gprintf( &x, &y, "Character size     : %d", textinfo.charsize );
  418.   gprintf( &x, &y, "Horizontal justify : %s", HorizJust[ textinfo.horiz ] );
  419.   gprintf( &x, &y, "Vertical justify   : %s", VertJust[ textinfo.vert ] );
  420.  
  421.   Pause();           /* Pause for user to read screen*/
  422.   return(0);
  423.  
  424. }
  425.  
  426.  
  427.  
  428. int static Textfont;
  429.  
  430. int TextDump2(void)
  431. {
  432.   static int CGASizes[]  = {
  433.     1, 3, 7, 3, 3   };
  434.   static int NormSizes[] = {
  435.     1, 4, 7, 4, 4   };
  436.  
  437.   char buffer[80];
  438.   int ch, wwidth, lwidth, size;
  439.   struct viewporttype vp;
  440.  
  441.     sprintf( buffer, "%s Character Set", Fonts[Textfont] );
  442.     MainWindow( buffer );     /* Display fontname as banner */
  443.     getviewsettings( &vp );      /* read current viewport   */
  444.  
  445.     settextjustify( LEFT_TEXT, TOP_TEXT );
  446.     moveto( 2, 3 );
  447.  
  448.     buffer[1] = '\0';                   /* Terminate string             */
  449.     wwidth = vp.right - vp.left; /* Determine the window width */
  450.     lwidth = textwidth( "H" );          /* Get average letter width     */
  451.  
  452.     if( Textfont == DEFAULT_FONT ){
  453.       changetextstyle( Textfont, HORIZ_DIR, 1 );
  454.       ch = 0;
  455.       while( ch < 256 ){      /* For each possible character   */
  456.    buffer[0] = ch;      /* Put character into a string   */
  457.    outtext( buffer );      /* send string to screen   */
  458.    if( (getx() + lwidth) > wwidth )
  459.      moveto( 2, gety() + textheight("H") + 3 );
  460.    ++ch;          /* Goto the next character */
  461.       }
  462.     }
  463.     else{
  464.  
  465.       size = (MaxY < 200) ? CGASizes[Textfont] : NormSizes[Textfont];
  466.       changetextstyle( Textfont, HORIZ_DIR, size );
  467.  
  468.       ch = '!';                         /* Begin at 1st printable       */
  469.       while( ch < 127 ){      /* For each printable character */
  470.    buffer[0] = ch;      /* Put character into a string   */
  471.    outtext( buffer );      /* send string to screen   */
  472.    if( (lwidth+getx()) > wwidth )   /* Are we still in window? */
  473.      moveto( 2, gety()+textheight("H")+3 );
  474.    ++ch;          /* Goto the next character */
  475.       }
  476.  
  477.     }
  478.  
  479.     Pause();            /* Pause until user acks   */
  480.     return(0);
  481. }  /* end of TextDump2 */
  482.  
  483. /*                         */
  484. /* TEXTDUMP: Display the all the characters in each of the  */
  485. /* available fonts.                 */
  486. /*                         */
  487.  
  488. int TextDump()
  489. {
  490.   for( Textfont=0 ; Textfont<5 ; ++Textfont ){  /* For each available font */
  491.    DrawAndPrint(TextDump2);
  492.   }               /* End of FONT loop     */
  493.   return(0);
  494. }
  495.  
  496. /*                         */
  497. /* BAR3DDEMO: Display a 3-D bar chart on the screen.     */
  498. /*                         */
  499.  
  500. int Bar3DDemo(void)
  501. {
  502.   static int barheight[] = {
  503.     1, 3, 5, 4, 3, 2, 1, 5, 4, 2, 3   };
  504.   struct viewporttype vp;
  505.   int xstep, ystep;
  506.   int i, j, h, color, bheight;
  507.   char buffer[10];
  508.  
  509.   MainWindow( "Bar 3-D / Rectangle Demonstration" );
  510.  
  511.   srand(0);
  512.   h = 3 * textheight( "H" );
  513.   getviewsettings( &vp );
  514.   settextjustify( CENTER_TEXT, TOP_TEXT );
  515.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  516.   outtextxy( MaxX/2, 6, "These are 3-D Bars" );
  517.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  518.   setviewport( vp.left+50, vp.top+40, vp.right-50, vp.bottom-10, 1 );
  519.   getviewsettings( &vp );
  520.  
  521.   line( h, h, h, vp.bottom-vp.top-h );
  522.   line( h, (vp.bottom-vp.top)-h, (vp.right-vp.left)-h, (vp.bottom-vp.top)-h );
  523.   xstep = ((vp.right-vp.left) - (2*h)) / 10;
  524.   ystep = ((vp.bottom-vp.top) - (2*h)) / 5;
  525.   j = (vp.bottom-vp.top) - h;
  526.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  527.  
  528.   for( i=0 ; i<6 ; ++i ){
  529.     line( h/2, j, h, j );
  530.     itoa( i, buffer, 10 );
  531.     outtextxy( 0, j, buffer );
  532.     j -= ystep;
  533.   }
  534.  
  535.   j = h;
  536.   settextjustify( CENTER_TEXT, TOP_TEXT );
  537.  
  538.   for( i=0 ; i<11 ; ++i ){
  539.     color = random( MaxColors-1 ) +1;
  540.     setfillstyle( i+1, color );
  541.     line( j, (vp.bottom-vp.top)-h, j, (vp.bottom-vp.top-3)-(h/2) );
  542.     itoa( i, buffer, 10 );
  543.     outtextxy( j, (vp.bottom-vp.top)-(h/2), buffer );
  544.     if( i != 10 ){
  545.       bheight = (vp.bottom-vp.top) - h - 1;
  546.       bar3d( j, (vp.bottom-vp.top-h)-(barheight[i]*ystep), j+xstep, bheight,
  547.              min(getmaxy(),getmaxx())/25, 1 );
  548.     }
  549.     j += xstep;
  550.   }
  551.  
  552.   Pause();           /* Pause for user's response    */
  553.   return(0);
  554.  
  555. }
  556.  
  557. /*                         */
  558. /* RANDOMBARS: Display random bars           */
  559. /*                         */
  560.  
  561. int RandomBars(void)
  562. {
  563.   int color;
  564.   int i;
  565.  
  566.   MainWindow( "Random Bars" );
  567.   /* StatusLine( "Esc aborts or press a key..." ); */
  568.   srand(0);
  569.   for ( i=0; i<10; i++ )
  570.   {
  571.     color = random( MaxColors-1 )+1;
  572.     setcolor( color );
  573.     setfillstyle( random(11)+1, color );
  574.     bar3d( random( getmaxx() ), random( getmaxy() ),
  575.       random( getmaxx() ), random( getmaxy() ), 0, OFF);
  576.   }
  577.  
  578.   Pause();           /* Pause for user's response    */
  579.   return(0);
  580.  
  581. }
  582.  
  583.  
  584.  
  585.  
  586. /*                         */
  587. /* TEXTDEMO2: Show given font in several sizes to the user.    */
  588. /*                         */
  589.  
  590. int TextDemo2(void)
  591. {
  592.   int charsize[] = {
  593.     1, 3, 7, 3, 4   };
  594.   int size;
  595.   int h, x, y, i;
  596.   struct viewporttype vp;
  597.   char buffer[80];
  598.  
  599.     sprintf( buffer, "%s Demonstration", Fonts[Textfont] );
  600.     MainWindow( buffer );
  601.     getviewsettings( &vp );
  602.  
  603.     changetextstyle( Textfont, VERT_DIR, charsize[Textfont] );
  604.     settextjustify( CENTER_TEXT, BOTTOM_TEXT );
  605.     outtextxy( 2*textwidth("M"), vp.bottom - 2*textheight("M"), "Vertical" );
  606.  
  607.     changetextstyle( Textfont, HORIZ_DIR, charsize[Textfont] );
  608.     settextjustify( LEFT_TEXT, TOP_TEXT );
  609.     outtextxy( 2*textwidth("M"), 2, "Horizontal" );
  610.  
  611.     settextjustify( CENTER_TEXT, CENTER_TEXT );
  612.     x = (vp.right - vp.left) / 2;
  613.     y = textheight( "H" );
  614.  
  615.     for( i=1 ; i<5 ; ++i ){      /* For each of the sizes */
  616.       size = (Textfont == SMALL_FONT) ? i+3 : i;
  617.       changetextstyle( Textfont, HORIZ_DIR, size );
  618.       h = textheight( "H" );
  619.       y += h;
  620.       sprintf( buffer, "Size %d", size );
  621.       outtextxy( x, y, buffer );
  622.  
  623.     }
  624.  
  625.     if( Textfont != DEFAULT_FONT ){    /* Show user declared Textfont size */
  626.       y += h / 2;       /* Move down the screen    */
  627.       settextjustify( CENTER_TEXT, TOP_TEXT );
  628.       setusercharsize( 5, 6, 3, 2 );
  629.       changetextstyle( Textfont, HORIZ_DIR, USER_CHAR_SIZE );
  630.       outtextxy( (vp.right-vp.left)/2, y, "User Defined Size" );
  631.     }
  632.  
  633.     Pause();            /* Pause to let user look  */
  634.     return(0);
  635. }  /* end of TextDemo2 */
  636.  
  637.  
  638. /*                         */
  639. /* TEXTDEMO: Show each font in several sizes to the user.      */
  640. /*                         */
  641.  
  642. int TextDemo(void)
  643. {
  644.  
  645.   for( Textfont=0 ; Textfont<5 ; ++Textfont ){  /* For each of the four fonts */
  646.    DrawAndPrint(TextDemo2);
  647.   }               /* End of FONT loop     */
  648.   return(0);
  649.  
  650. }
  651.  
  652. /*                         */
  653. /* COLORDEMO: Display the current color palette on the screen. */
  654. /*                         */
  655.  
  656. int ColorDemo(void)
  657. {
  658.   struct viewporttype vp;
  659.   int color, height, width;
  660.   int x, y, i, j;
  661.   char cnum[5];
  662.  
  663.   MainWindow( "Color Demonstration" );  /* Show demonstration name      */
  664.  
  665.   color = 1;
  666.   getviewsettings( &vp );     /* Get the current window size   */
  667.   width  = 2 * ( (vp.right+1) / 16 );     /* Get box dimensions      */
  668.   height = 2 * ( (vp.bottom-10) / 10 );
  669.  
  670.   x = width / 2;
  671.   y = height / 2; /* Leave 1/2 box border    */
  672.  
  673.   for( j=0 ; j<3 ; ++j ){     /* Row loop       */
  674.  
  675.     for( i=0 ; i<5 ; ++i ){      /* Column loop       */
  676.  
  677.       setfillstyle(SOLID_FILL, color); /* Set to solid fill in color */
  678.       setcolor( color );      /* Set the same border color  */
  679.  
  680.       bar( x, y, x+width, y+height );  /* Draw the rectangle      */
  681.       rectangle( x, y, x+width, y+height );  /* outline the rectangle   */
  682.  
  683.       if( color == BLACK ){      /* If box was black...     */
  684.    setcolor( WHITE );      /* Set drawing color to white */
  685.    rectangle( x, y, x+width, y+height );  /* Outline black in white*/
  686.       }
  687.  
  688.       itoa( color, cnum, 10 );      /* Convert # to ASCII      */
  689.       outtextxy( x+(width/2), y+height+4, cnum );  /* Show color #   */
  690.  
  691.       color = ++color % MaxColors;  /* Advance to the next color  */
  692.       x += (width / 2) * 3;      /* move the column base    */
  693.     }          /* End of Column loop      */
  694.  
  695.     y += (height / 2) * 3;    /* move the row base    */
  696.     x = width / 2;         /* reset column base    */
  697.   }               /* End of Row loop      */
  698.  
  699.   Pause();           /* Pause for user's response    */
  700.   return(0);
  701.  
  702. }
  703.  
  704. /*                         */
  705. /* ARCDEMO: Display a random pattern of arcs on the screen */
  706. /* until the user says enough.               */
  707. /*                         */
  708.  
  709. int ArcDemo(void)
  710. {
  711.   int mradius;          /* Maximum radius allowed  */
  712.   int eangle;           /* Random end angle of Arc */
  713.   struct arccoordstype ai;    /* Used to read Arc Cord info */
  714.    int   i;
  715.  
  716.   MainWindow( "Arc Demonstration" );
  717.   /* StatusLine( "ESC Aborts - Press a Key to stop" ); */
  718.  
  719.   mradius = MaxY / 10;        /* Determine the maximum radius */
  720.  
  721.   srand(0); /* to draw always the same ( for the same MaxX,MaxY ) */
  722.   for ( i=0; i<100; i++ )
  723.   {
  724.     setcolor( random( MaxColors - 1 ) + 1 ); /* Randomly select a color */
  725.     eangle = random( 358 ) + 1;  /* Select an end angle     */
  726.      arc( random(MaxX), random(MaxY), random(eangle), eangle, mradius );
  727.     getarccoords( &ai );      /* Read Cord data    */
  728.     line( ai.x, ai.y, ai.xstart, ai.ystart ); /* line from start to center */
  729.     line( ai.x, ai.y,   ai.xend,   ai.yend ); /* line from end to center   */
  730.   }
  731.  
  732.   Pause();           /* Wait for user's response     */
  733.   return(0);
  734.  
  735. }
  736.  
  737. /*                         */
  738. /* CIRCLEDEMO: Display a random pattern of circles on the screen  */
  739. /* until the user says enough.               */
  740. /*                         */
  741.  
  742. int CircleDemo(void)
  743. {
  744.   int mradius;          /* Maximum radius allowed  */
  745.   int i;
  746.  
  747.   MainWindow( "Circle Demonstration" );
  748.   /* StatusLine( "ESC Aborts - Press a Key to stop" ); */
  749.  
  750.   srand(0);
  751.   mradius = MaxY / 10;        /* Determine the maximum radius */
  752.  
  753.   for ( i=0; i<100; i++ )
  754.   {
  755.     setcolor( random( MaxColors - 1 ) + 1 ); /* Randomly select a color */
  756.     circle( random(MaxX), random(MaxY), random(mradius) );
  757.   }               /* End of WHILE not KBHIT  */
  758.  
  759.   Pause();           /* Wait for user's response     */
  760.   return(0);
  761.  
  762. }
  763.  
  764. /*                         */
  765. /* PIEDEMO: Display a pie chart on the screen.        */
  766. /*                         */
  767.  
  768. #define adjasp( y )  ((int)(AspectRatio * (double)(y)))
  769. #define torad( d )   (( (double)(d) * PI ) / 180.0 )
  770.  
  771. int PieDemo(void)
  772. {
  773.   struct viewporttype vp;
  774.   int xcenter, ycenter, radius, lradius;
  775.   int x, y;
  776.   double radians, piesize;
  777.  
  778.   MainWindow( "Pie Chart Demonstration" );
  779.  
  780.   getviewsettings( &vp );     /* Get the current viewport   */
  781.   xcenter = (vp.right - vp.left) / 2;  /* Center the Pie horizontally   */
  782.   ycenter = (vp.bottom - vp.top) / 2+20;/* Center the Pie vertically */
  783.   radius  = (vp.bottom - vp.top) / 3;  /* It will cover 2/3rds screen   */
  784.   piesize = (vp.bottom - vp.top) / 4.0; /* Optimum height ratio of pie  */
  785.  
  786.   while( (AspectRatio*radius) < piesize ) ++radius;
  787.  
  788.   lradius = radius + ( radius / 5 );   /* Labels placed 20% farther  */
  789.  
  790.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  791.   settextjustify( CENTER_TEXT, TOP_TEXT );
  792.   outtextxy( MaxX/2, 6, "This is a Pie Chart" );
  793.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 1 );
  794.   settextjustify( CENTER_TEXT, TOP_TEXT );
  795.  
  796.   setfillstyle( SOLID_FILL, RED );
  797.   pieslice( xcenter+10, ycenter-adjasp(10), 0, 90, radius );
  798.   radians = torad( 45 );
  799.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  800.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  801.   settextjustify( LEFT_TEXT, BOTTOM_TEXT );
  802.   outtextxy( x, y, "25 %" );
  803.  
  804.   setfillstyle( WIDE_DOT_FILL, GREEN );
  805.   pieslice( xcenter, ycenter, 90, 135, radius );
  806.   radians = torad( 113 );
  807.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  808.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  809.   settextjustify( RIGHT_TEXT, BOTTOM_TEXT );
  810.   outtextxy( x, y, "12.5 %" );
  811.  
  812.   setfillstyle( INTERLEAVE_FILL, YELLOW );
  813.   settextjustify( RIGHT_TEXT, CENTER_TEXT );
  814.   pieslice( xcenter-10, ycenter, 135, 225, radius );
  815.   radians = torad( 180 );
  816.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  817.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  818.   settextjustify( RIGHT_TEXT, CENTER_TEXT );
  819.   outtextxy( x, y, "25 %" );
  820.  
  821.   setfillstyle( HATCH_FILL, BLUE );
  822.   pieslice( xcenter, ycenter, 225, 360, radius );
  823.   radians = torad( 293 );
  824.   x = xcenter + (int)( cos( radians ) * (double)lradius );
  825.   y = ycenter - (int)( sin( radians ) * (double)lradius * AspectRatio );
  826.   settextjustify( LEFT_TEXT, TOP_TEXT );
  827.   outtextxy( x, y, "37.5 %" );
  828.  
  829.   Pause();           /* Pause for user's response    */
  830.   return(0);
  831.  
  832. }
  833.  
  834. /*                         */
  835. /* BARDEMO: Draw a 2-D bar chart using Bar and Rectangle.      */
  836. /*                         */
  837.  
  838. int BarDemo(void)
  839. {
  840.   int barheight[] = {
  841.     1, 3, 5, 2, 4   };
  842.   int styles[]   = {
  843.     1, 3, 10, 5, 9, 1   };
  844.   int xstep, ystep;
  845.   int sheight, swidth;
  846.   int i, j, h;
  847.   struct viewporttype vp;
  848.   char buffer[40];
  849.  
  850.   MainWindow( "Bar / Rectangle demonstration" );
  851.   srand(0);
  852.   h = 3 * textheight( "H" );
  853.   getviewsettings( &vp );
  854.   settextjustify( CENTER_TEXT, TOP_TEXT );
  855.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  856.   outtextxy( MaxX /2, 6, "These are 2-D Bars" );
  857.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  858.   setviewport( vp.left+50, vp.top+30, vp.right-50, vp.bottom-10, 1 );
  859.  
  860.   getviewsettings( &vp );
  861.   sheight = vp.bottom - vp.top;
  862.   swidth  = vp.right  - vp.left;
  863.  
  864.   line( h, h, h, sheight-h );
  865.   line( h, sheight-h, sheight-h, sheight-h );
  866.   ystep = (sheight - (2*h) ) / 5;
  867.   xstep = (swidth  - (2*h) ) / 5;
  868.   j = sheight - h;
  869.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  870.  
  871.   for( i=0 ; i<6 ; ++i ){
  872.     line( h/2, j, h, j );
  873.     itoa( i, buffer, 10 );
  874.     outtextxy( 0, j, buffer );
  875.     j -= ystep;
  876.   }
  877.  
  878.   j = h;
  879.   settextjustify( CENTER_TEXT, TOP_TEXT );
  880.   for( i=0 ; i<6 ; ++i ){
  881.     setfillstyle( styles[i], random(MaxColors-1)+1 );
  882.     line( j, sheight - h, j, sheight- 3 - (h/2) );
  883.     itoa( i, buffer, 10 );
  884.     outtextxy( j, sheight - (h/2), buffer );
  885.     if( i != 5 ){
  886.       bar( j, (sheight-h)-(barheight[i] * ystep), j+xstep, sheight-h-1 );
  887.       rectangle( j, (sheight-h)-(barheight[i] * ystep), j+xstep, sheight-h);
  888.     }
  889.     j += xstep;
  890.   }
  891.  
  892.   Pause();
  893.   return(0);
  894.  
  895. }
  896.  
  897. /*                         */
  898. /* LINERELDEMO: Display pattern using moverel and linerel cmds.   */
  899. /*                         */
  900.  
  901. int LineRelDemo(void)
  902. {
  903.   struct viewporttype vp;
  904.   int h, w, dx, dy, cx, cy;
  905.   struct PTS outs[7];
  906.  
  907.  
  908.   MainWindow( "MoveRel / LineRel Demonstration" );
  909.   StatusLine( "Press any key to continue, ESC to Abort" );
  910.  
  911.   getviewsettings( &vp );
  912.   cx = (vp.right  - vp.left) / 2;   /* Center of the screen coords   */
  913.   cy = (vp.bottom - vp.top ) / 2;
  914.  
  915.   h  = (vp.bottom - vp.top ) / 8;
  916.   w  = (vp.right  - vp.left) / 9;
  917.  
  918.   dx = 2 * w;
  919.   dy = 2 * h;
  920.  
  921.   setcolor( BLACK );
  922.  
  923.   setfillstyle( SOLID_FILL, BLUE );
  924.   bar( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /* Draw backgnd */
  925.  
  926.   outs[0].x = cx -  dx;
  927.   outs[0].y = cy -  dy;
  928.   outs[1].x = cx - (dx-w);
  929.   outs[1].y = cy - (dy+h);
  930.   outs[2].x = cx +  dx;
  931.   outs[2].y = cy - (dy+h);
  932.   outs[3].x = cx +  dx;
  933.   outs[3].y = cy +  dy;
  934.   outs[4].x = cx + (dx-w);
  935.   outs[4].y = cy + (dy+h);
  936.   outs[5].x = cx -  dx;
  937.   outs[5].y = cy + (dy+h);
  938.   outs[6].x = cx -  dx;
  939.   outs[6].y = cy -  dy;
  940.  
  941.   setfillstyle( SOLID_FILL, WHITE );
  942.   fillpoly( 7, (int far *)outs );
  943.  
  944.   outs[0].x = cx - (w/2);
  945.   outs[0].y = cy + h;
  946.   outs[1].x = cx + (w/2);
  947.   outs[1].y = cy + h;
  948.   outs[2].x = cx + (w/2);
  949.   outs[2].y = cy - h;
  950.   outs[3].x = cx - (w/2);
  951.   outs[3].y = cy - h;
  952.   outs[4].x = cx - (w/2);
  953.   outs[4].y = cy + h;
  954.  
  955.   setfillstyle( SOLID_FILL, BLUE );
  956.   fillpoly( 5, (int far *)outs );
  957.  
  958.   /*  Draw a Tesseract object on the screen using the LineRel and */
  959.   /*  MoveRel drawing commands.              */
  960.  
  961.   moveto( cx-dx, cy-dy );
  962.   linerel(  w, -h );
  963.   linerel(  3*w,  0 );
  964.   linerel(   0,  5*h );
  965.   linerel( -w, h );
  966.   linerel( -3*w,  0 );
  967.   linerel(   0, -5*h );
  968.  
  969.   moverel( w, -h );
  970.   linerel(   0,  5*h );
  971.   linerel( w+(w/2), 0 );
  972.   linerel(   0, -3*h );
  973.   linerel( w/2,   -h );
  974.   linerel( 0, 5*h );
  975.  
  976.   moverel(  0, -5*h );
  977.   linerel( -(w+(w/2)), 0 );
  978.   linerel( 0, 3*h );
  979.   linerel( -w/2, h );
  980.  
  981.   moverel( w/2, -h );
  982.   linerel( w, 0 );
  983.  
  984.   moverel( 0, -2*h );
  985.   linerel( -w, 0 );
  986.  
  987.   Pause();           /* Wait for user's response     */
  988.   return(0);
  989.  
  990. }
  991.  
  992. /*                         */
  993. /* PUTPIXELDEMO: Display a pattern of random dots on the screen   */
  994. /* and pick them back up again.              */
  995. /*                         */
  996.  
  997. int PutPixelDemo(void)
  998. {
  999.   int seed = 1958;
  1000.   int i, x, y, h, w, color;
  1001.   struct viewporttype vp;
  1002.  
  1003.   MainWindow( "PutPixel / GetPixel Demonstration" );
  1004.  
  1005.   getviewsettings( &vp );
  1006.   h = vp.bottom - vp.top;
  1007.   w = vp.right - vp.left;
  1008.  
  1009.   srand( seed );        /* Restart random # function  */
  1010.  
  1011.   for( i=0 ; i<5000 ; ++i ){     /* Put 5000 pixels on screen  */
  1012.     x = 1 + random( w - 1 );     /* Generate a random location */
  1013.     y = 1 + random( h - 1 );
  1014.     color = random( MaxColors );
  1015.     putpixel( x, y, color );
  1016.   }
  1017.  
  1018.   srand( seed );        /* Restart Random # at same # */
  1019.  
  1020.   for( i=0 ; i<5000 ; ++i ){     /* Take the 5000 pixels off   */
  1021.     x = 1 + random( w - 1 );     /* Generate a random location */
  1022.     y = 1 + random( h - 1 );
  1023.     color = getpixel( x, y );    /* Read the color pixel    */
  1024.      /* color = random( MaxColors ); */
  1025.     if( color == random( MaxColors ) )    /* Used to keep RANDOM in sync   */
  1026.       putpixel( x, y, 0 );    /* Write pixel to BLACK    */
  1027.     else
  1028.       x=color;
  1029.   }
  1030.  
  1031.   Pause();           /* Wait for user's response     */
  1032.   return(0);
  1033.  
  1034. }
  1035.  
  1036. /*                         */
  1037. /*   PUTIMAGEDEMO                   */
  1038. /*                         */
  1039. int PutImageDemo(void)
  1040. {
  1041.   static int r     = 20;
  1042.   static int StartX = 100;
  1043.   static int StartY = 50;
  1044.  
  1045.   struct viewporttype vp;
  1046.   int PauseTime, x, y, ulx, uly, lrx, lry, size, i, width, height, step;
  1047.   void *Saucer;
  1048.  
  1049.   MainWindow("GetImage / PutImage Demonstration");
  1050.   getviewsettings( &vp );
  1051.  
  1052.   /* Draw Saucer */
  1053.   setfillstyle( SOLID_FILL, getmaxcolor() );
  1054.   fillellipse(StartX, StartY, r, (r/3)+2);
  1055.   ellipse(StartX, StartY-4, 190, 357, r, r/3);
  1056.  
  1057.   line(StartX+7, StartY-6, StartX+10, StartY-12);
  1058.   circle(StartX+10, StartY-12, 2);
  1059.   line(StartX-7, StartY-6, StartX-10, StartY-12);
  1060.   circle(StartX-10, StartY-12, 2);
  1061.  
  1062.  
  1063.   /* Read saucer image */
  1064.   ulx = StartX-(r+1);
  1065.   uly = StartY-14;
  1066.   lrx = StartX+(r+1);
  1067.   lry = StartY+(r/3)+3;
  1068.   width = lrx - ulx + 1;
  1069.   height = lry - uly + 1;
  1070.   size = imagesize(ulx, uly, lrx, lry);
  1071.  
  1072.   Saucer = malloc( size );
  1073.   getimage(ulx, uly, lrx, lry, Saucer);
  1074.   putimage(ulx, uly, Saucer, XOR_PUT);
  1075.  
  1076. /* Plot some "stars"  */
  1077.   for ( i=0 ; i<1000; ++i )
  1078.     putpixel(random(MaxX), random(MaxY), random( MaxColors-1 )+1);
  1079.   x = MaxX / 2;
  1080.   y = MaxY / 2;
  1081.   PauseTime = 70;
  1082.  
  1083.   /* until a key is hit */
  1084.   for ( i=0 ; i<20; ++i )
  1085.   {
  1086.     /* Draw the Saucer */
  1087.     putimage(x, y, Saucer, XOR_PUT);           /*  draw image  */
  1088.     delay(PauseTime);
  1089.     putimage(x, y, Saucer, XOR_PUT);           /* erase image  */
  1090.  
  1091.     /* Move Saucer */
  1092.  
  1093.     step = random( 2*r );
  1094.     if ((step/2) % 2 != 0 )
  1095.       step = -1 * step;
  1096.     x = x + step;
  1097.     step = random( r );
  1098.     if ((step/2) % 2 != 0 )
  1099.       step = -1 * step;
  1100.     y = y + step;
  1101.  
  1102.     if (vp.left + x + width - 1 > vp.right)
  1103.       x = vp.right-vp.left-width + 1;
  1104.     else
  1105.       if (x < 0)
  1106.    x = 0;
  1107.     if (vp.top + y + height - 1 > vp.bottom)
  1108.       y = vp.bottom-vp.top-height + 1;
  1109.     else
  1110.       if (y < 0)
  1111.    y = 0;
  1112.   }
  1113.   free( Saucer );
  1114.   Pause();
  1115.   return(0);
  1116. }
  1117.  
  1118.  
  1119. /*                         */
  1120. /* LINETODEMO: Display a pattern using moveto and lineto commands. */
  1121. /*                         */
  1122.  
  1123. #define MAXPTS 15
  1124.  
  1125. int LineToDemo(void)
  1126. {
  1127.   struct viewporttype vp;
  1128.   struct PTS points[MAXPTS];
  1129.   int i, j, h, w, xcenter, ycenter;
  1130.   int radius, angle, step;
  1131.   double  rads;
  1132.  
  1133.   MainWindow( "MoveTo / LineTo Demonstration" );
  1134.  
  1135.   getviewsettings( &vp );
  1136.   h = vp.bottom - vp.top;
  1137.   w = vp.right - vp.left;
  1138.  
  1139.   xcenter = w / 2;         /* Determine the center of circle */
  1140.   ycenter = h / 2;
  1141.   radius  = (h - 30) / (AspectRatio * 2);
  1142.   step     = 360 / MAXPTS;    /* Determine # of increments  */
  1143.  
  1144.   angle = 0;            /* Begin at zero degrees   */
  1145.   for( i=0 ; i<MAXPTS ; ++i ){      /* Determine circle intercepts   */
  1146.     rads = (double)angle * PI / 180.0; /* Convert angle to radians   */
  1147.     points[i].x = xcenter + (int)( cos(rads) * radius );
  1148.     points[i].y = ycenter - (int)( sin(rads) * radius * AspectRatio );
  1149.     angle += step;         /* Move to next increment  */
  1150.   }
  1151.  
  1152.   circle( xcenter, ycenter, radius );  /* Draw bounding circle    */
  1153.  
  1154.   for( i=0 ; i<MAXPTS ; ++i ){      /* Draw the cords to the circle */
  1155.     for( j=i ; j<MAXPTS ; ++j ){ /* For each remaining intersect */
  1156.       moveto(points[i].x, points[i].y); /* Move to beginning of cord */
  1157.       lineto(points[j].x, points[j].y); /* Draw the cord    */
  1158.     }
  1159.   }
  1160.  
  1161.   Pause();           /* Wait for user's response     */
  1162.   return(0);
  1163.  
  1164. }
  1165.  
  1166. /*                         */
  1167. /* LINESTYLEDEMO: Display a pattern using all of the standard  */
  1168. /* line styles that are available.           */
  1169. /*                         */
  1170.  
  1171. int LineStyleDemo(void)
  1172. {
  1173.   int style, step;
  1174.   int x, y, w;
  1175.   struct viewporttype vp;
  1176.   char buffer[40];
  1177.  
  1178.   MainWindow( "Pre-defined line styles" );
  1179.  
  1180.   getviewsettings( &vp );
  1181.   w = vp.right - vp.left;
  1182.  
  1183.   x = 35;
  1184.   y = 10;
  1185.   step = w / 11;
  1186.  
  1187.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1188.   outtextxy( x, y, "Normal Width" );
  1189.  
  1190.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1191.  
  1192.   for( style=0 ; style<4 ; ++style ){
  1193.     setlinestyle( style, 0, NORM_WIDTH );
  1194.     line( x, y+20, x, vp.bottom-40 );
  1195.     itoa( style, buffer, 10 );
  1196.     outtextxy( x, vp.bottom-30, buffer );
  1197.     x += step;
  1198.   }
  1199.  
  1200.   x += 2 * step;
  1201.  
  1202.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1203.   outtextxy( x, y, "Thick Width" );
  1204.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1205.  
  1206.   for( style=0 ; style<4 ; ++style ){
  1207.     setlinestyle( style, 0, THICK_WIDTH );
  1208.     line( x, y+20, x, vp.bottom-40 );
  1209.     itoa( style, buffer, 10 );
  1210.     outtextxy( x, vp.bottom-30, buffer );
  1211.     x += step;
  1212.   }
  1213.  
  1214.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1215.  
  1216.   Pause();           /* Wait for user's response     */
  1217.   return(0);
  1218.  
  1219. }
  1220.  
  1221. /*                         */
  1222. /* CRTMODEDEMO: Demonstrate the effects of the change mode  */
  1223. /* commands on the current screen.           */
  1224. /*                         */
  1225.  
  1226. int CRTModeDemo(void)
  1227. {
  1228.   struct viewporttype vp;
  1229.   int mode;
  1230.  
  1231.   MainWindow( "SetGraphMode / RestoreCRTMode demo" );
  1232.   getviewsettings( &vp );
  1233.   mode = getgraphmode();
  1234.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  1235.  
  1236.   outtextxy( (vp.right-vp.left)/2, (vp.bottom-vp.top)/2,
  1237.   "Now you are in graphics mode..." );
  1238.   StatusLine( "Press any key for text mode..." );
  1239.   getch();
  1240.  
  1241.   restorecrtmode();
  1242.   printf( "Now you are in text mode.\n\n" );
  1243.   printf( "Press any key to go back to graphics..." );
  1244.   getch();
  1245.  
  1246.   setgraphmode( mode );
  1247.   MainWindow( "SetGraphMode / RestoreCRTMode demo" );
  1248.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  1249.   outtextxy( (vp.right-vp.left)/2, (vp.bottom-vp.top)/2,
  1250.   "Back in Graphics Mode..." );
  1251.  
  1252.   Pause();           /* Wait for user's response     */
  1253.   return(0);
  1254.  
  1255. }
  1256.  
  1257. /*                         */
  1258. /* USERLINESTYLEDEMO: Display line styles showing the user  */
  1259. /* defined line style functions.             */
  1260. /*                         */
  1261.  
  1262. int UserLineStyleDemo(void)
  1263. {
  1264.   int x, y, i, h, flag;
  1265.   unsigned int style;
  1266.   struct viewporttype vp;
  1267.  
  1268.   MainWindow( "User defined line styles" );
  1269.  
  1270.   getviewsettings( &vp );
  1271.   h = vp.bottom - vp.top;
  1272.  
  1273.   x = 4;
  1274.   y = 10;
  1275.   style = 0;
  1276.   i = 0;
  1277.  
  1278.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1279.   flag = TRUE;          /* Set the bits in this pass  */
  1280.  
  1281.   while( x < vp.right-2 ){    /* Draw lines across the screen */
  1282.  
  1283.     if( flag )          /* If flag, set bits...    */
  1284.       style = style | (1 << i);  /*    Set the Ith bit in word */
  1285.     else          /* If no flag, clear bits  */
  1286.     style = style & !(0x8000 >> i); /*    Clear the Ith bit in word */
  1287.  
  1288.     setlinestyle( USERBIT_LINE, style, NORM_WIDTH );
  1289.     line( x, y, x, h-y );     /* Draw the new line pattern  */
  1290.  
  1291.     x += 5;          /* Move the X location of line   */
  1292.     i = ++i % 16;       /* Advance to next bit pattern   */
  1293.  
  1294.     if( style == 0xffff ){    /* Are all bits set?    */
  1295.       flag = FALSE;        /*   begin removing bits   */
  1296.       i = 0;            /* Start with whole pattern   */
  1297.     }
  1298.     else{            /* Bits not all set...     */
  1299.       if( style == 0 )        /* Are all bits clear?     */
  1300.    flag = TRUE;         /*   begin setting bits    */
  1301.     }
  1302.   }
  1303.  
  1304.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1305.  
  1306.   Pause();           /* Wait for user's response     */
  1307.   return(0);
  1308.  
  1309. }
  1310.  
  1311. /*                         */
  1312. /* FILLSTYLEDEMO: Display the standard fill patterns available.   */
  1313. /*                         */
  1314.  
  1315. int FillStyleDemo(void)
  1316. {
  1317.   int h, w, style;
  1318.   int i, j, x, y;
  1319.   struct viewporttype vp;
  1320.   char buffer[40];
  1321.  
  1322.   MainWindow( "Pre-defined Fill Styles" );
  1323.  
  1324.   getviewsettings( &vp );
  1325.   w = 2 * ((vp.right  +  1) / 13);
  1326.   h = 2 * ((vp.bottom - 10) / 10);
  1327.  
  1328.   x = w / 2;
  1329.   y = h / 2;      /* Leave 1/2 blk margin    */
  1330.   style = 0;
  1331.  
  1332.   for( j=0 ; j<3 ; ++j ){     /* Three rows of boxes     */
  1333.     for( i=0 ; i<4 ; ++i ){      /* Four column of boxes    */
  1334.       setfillstyle(style, MaxColors-1); /* Set the fill style and WHITE */
  1335.       bar( x, y, x+w, y+h );     /* Draw the actual box     */
  1336.       rectangle( x, y, x+w, y+h );  /* Outline the box      */
  1337.       itoa( style, buffer, 10 ); /* Convert style 3 to ASCII   */
  1338.       outtextxy( x+(w / 2), y+h+4, buffer );
  1339.       ++style;          /* Go on to next style #   */
  1340.       x += (w / 2) * 3;       /* Go to next column    */
  1341.     }          /* End of coulmn loop      */
  1342.     x = w / 2;          /* Put base back to 1st column   */
  1343.     y += (h / 2) * 3;         /* Advance to next row     */
  1344.   }               /* End of Row loop      */
  1345.  
  1346.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1347.  
  1348.   Pause();           /* Wait for user's response     */
  1349.   return(0);
  1350.  
  1351. }
  1352.  
  1353. /*                         */
  1354. /* FILLPATTERNDEMO: Demonstrate how to use the user definable  */
  1355. /* fill patterns.                   */
  1356. /*                         */
  1357.  
  1358. int FillPatternDemo(void)
  1359. {
  1360.   int style;
  1361.   int h, w;
  1362.   int x, y, i, j;
  1363.   char buffer[40];
  1364.   struct viewporttype vp;
  1365.   static char patterns[][8] = {
  1366.     { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 },
  1367.     { 0x33, 0x33, 0xCC, 0xCC, 0x33, 0x33, 0xCC, 0xCC },
  1368.     { 0xF0, 0xF0, 0xF0, 0xF0, 0x0F, 0x0F, 0x0F, 0x0F },
  1369.     { 0x00, 0x10, 0x28, 0x44, 0x28, 0x10, 0x00, 0x00 },
  1370.     { 0x00, 0x70, 0x20, 0x27, 0x24, 0x24, 0x07, 0x00 },
  1371.     { 0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00 },
  1372.     { 0x00, 0x00, 0x3C, 0x3C, 0x3C, 0x3C, 0x00, 0x00 },
  1373.     { 0x00, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x7E, 0x00 },
  1374.     { 0x00, 0x00, 0x22, 0x08, 0x00, 0x22, 0x1C, 0x00 },
  1375.     { 0xFF, 0x7E, 0x3C, 0x18, 0x18, 0x3C, 0x7E, 0xFF },
  1376.     { 0x00, 0x10, 0x10, 0x7C, 0x10, 0x10, 0x00, 0x00 },
  1377.     { 0x00, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x00 }
  1378.   };
  1379.  
  1380.   MainWindow( "User Defined Fill Styles" );
  1381.  
  1382.   getviewsettings( &vp );
  1383.   w = 2 * ((vp.right  +  1) / 13);
  1384.   h = 2 * ((vp.bottom - 10) / 10);
  1385.  
  1386.   x = w / 2;
  1387.   y = h / 2;      /* Leave 1/2 blk margin    */
  1388.   style = 0;
  1389.  
  1390.   for( j=0 ; j<3 ; ++j ){     /* Three rows of boxes     */
  1391.     for( i=0 ; i<4 ; ++i ){      /* Four column of boxes    */
  1392.       setfillpattern( &patterns[style][0], MaxColors-1 );
  1393.       bar( x, y, x+w, y+h );     /* Draw the actual box     */
  1394.       rectangle( x, y, x+w, y+h );  /* Outline the box      */
  1395.       itoa( style, buffer, 10 ); /* Convert style 3 to ASCII   */
  1396.       outtextxy( x+(w / 2), y+h+4, buffer );
  1397.       ++style;          /* Go on to next style #   */
  1398.       x += (w / 2) * 3;       /* Go to next column    */
  1399.     }          /* End of coulmn loop      */
  1400.     x = w / 2;          /* Put base back to 1st column   */
  1401.     y += (h / 2) * 3;         /* Advance to next row     */
  1402.   }               /* End of Row loop      */
  1403.  
  1404.   settextjustify( LEFT_TEXT, TOP_TEXT );
  1405.  
  1406.   Pause();           /* Wait for user's response     */
  1407.   return(0);
  1408.  
  1409. }
  1410.  
  1411. /*                         */
  1412. /* POLYDEMO: Display a random pattern of polygons on the screen   */
  1413. /* until the user says enough.               */
  1414. /*                         */
  1415.  
  1416. int PaletteDemo(void)
  1417. {
  1418.   int i, j, x, y, color;
  1419.   struct viewporttype vp;
  1420.   int height, width;
  1421.  
  1422.   MainWindow( "Palette Demonstration" );
  1423.   StatusLine( "Press any key to continue, ESC to Abort" );
  1424.  
  1425.   srand(0);
  1426.   getviewsettings( &vp );
  1427.   width  = (vp.right - vp.left) / 15;  /* get width of the box    */
  1428.   height = (vp.bottom - vp.top) / 10;  /* Get the height of the box  */
  1429.  
  1430.   x = y = 0;            /* Start in upper corner   */
  1431.   color = 1;            /* Begin at 1st color      */
  1432.  
  1433.   for( j=0 ; j<10 ; ++j ){    /* For 10 rows of boxes    */
  1434.     for( i=0 ; i<15 ; ++i ){     /* For 15 columns of boxes */
  1435.       setfillstyle( SOLID_FILL, color );  /* Set the color of box */
  1436.       bar( x, y, x+width, y+height );     /* Draw the box   */
  1437.       x += width + 1;            /* Advance to next col  */
  1438.       color = 1 + (color % (MaxColors - 2)); /* Set new color  */
  1439.     }          /* End of COLUMN loop      */
  1440.     x = 0;           /* Goto 1st column      */
  1441.     y += height + 1;       /* Goto next row     */
  1442.   }               /* End of ROW loop      */
  1443.  
  1444.   while( !kbhit() ){       /* Until user enters a key... */
  1445.     if ( GraphDriver==VGA )
  1446.        setrgbpalette( 1+random(MaxColors - 2), random(65),random(65),random(65) );
  1447.     else
  1448.       setpalette( 1+random(MaxColors - 2), random( 65 ) );
  1449.   }
  1450.  
  1451.   setallpalette( &palette );
  1452.  
  1453.   Pause();           /* Wait for user's response     */
  1454.   return(0);
  1455.  
  1456. }
  1457.  
  1458. /*                         */
  1459. /* POLYDEMO: Display a random pattern of polygons on the screen   */
  1460. /* until the user says enough.               */
  1461. /*                         */
  1462.  
  1463. #define MaxPts    6     /* Maximum # of pts in polygon   */
  1464.  
  1465. int PolyDemo(void)
  1466. {
  1467.   struct PTS poly[ MaxPts ];     /* Space to hold datapoints   */
  1468.   int color;            /* Current drawing color   */
  1469.   int i,j;
  1470.  
  1471.   MainWindow( "DrawPoly / FillPoly Demonstration" );
  1472.   /* StatusLine( "ESC Aborts - Press a Key to stop" ); */
  1473.  
  1474.   srand(0);
  1475.   for ( j=0; j<5; j++ )
  1476.   {
  1477.  
  1478.     color = 1 + random( MaxColors-1 ); /* Get a random color # (no blk)*/
  1479.     setfillstyle( random(10), color ); /* Set a random line style */
  1480.     setcolor( color );        /* Set the desired color   */
  1481.  
  1482.     for( i=0 ; i<(MaxPts-1) ; i++ ){   /* Determine a random polygon */
  1483.       poly[i].x = random( MaxX );   /* Set the x coord of point   */
  1484.       poly[i].y = random( MaxY );   /* Set the y coord of point   */
  1485.     }
  1486.  
  1487.     poly[i].x = poly[0].x;    /* last point = first point   */
  1488.     poly[i].y = poly[1].y;
  1489.  
  1490.     fillpoly( MaxPts, (int far *)poly );    /* Draw the actual polygon      */
  1491.   }               /* End of WHILE not KBHIT  */
  1492.  
  1493.   Pause();           /* Wait for user's response     */
  1494.   return(0);
  1495.  
  1496. }
  1497.  
  1498.  
  1499. /*                         */
  1500. /* SAYGOODBYE: Give a closing screen to the user before leaving.  */
  1501. /*                         */
  1502.  
  1503. int SayGoodbye(void)
  1504. {
  1505.   struct viewporttype viewinfo;  /* Structure to read viewport */
  1506.   int h, w;
  1507.  
  1508.   MainWindow( "== Finale ==" );
  1509.  
  1510.   getviewsettings( &viewinfo );  /* Read viewport settings  */
  1511.   changetextstyle( TRIPLEX_FONT, HORIZ_DIR, 4 );
  1512.   settextjustify( CENTER_TEXT, CENTER_TEXT );
  1513.  
  1514.   h = viewinfo.bottom - viewinfo.top;
  1515.   w = viewinfo.right  - viewinfo.left;
  1516.   outtextxy( w/2, h/2, "That's all, folks!" );
  1517.  
  1518.   StatusLine( "Press any key to EXIT" );
  1519.   getch();
  1520.  
  1521.   cleardevice();        /* Clear the graphics screen  */
  1522.    return(0);
  1523. }
  1524.  
  1525. /*                         */
  1526. /* PAUSE: Pause until the user enters a keystroke. If the      */
  1527. /* key is an ESC, then exit program, else simply return.    */
  1528. /*                         */
  1529.  
  1530. void Pause(void)
  1531. {
  1532.   static char msg[] = "Esc aborts, Ctrl-P prints, other key continue ...";
  1533.   int c;
  1534.  
  1535.   StatusLine( msg );       /* Put msg at bottom of screen   */
  1536.  
  1537.    if ( !printing )
  1538.    {
  1539.      c = getch();          /* Read a character from kbd  */
  1540.      if( 0 == c )          /* Did use hit a non-ASCII key? */
  1541.        c = getch();        /* Read scan code for keyboard   */
  1542.      else
  1543.      {
  1544.         if( ESC == c ){       /* Does user wish to leave?   */
  1545.           closegraph();       /* Change to text mode     */
  1546.           exit( 1 );          /* Return to OS      */
  1547.         }
  1548.         if ( c == 16 ) /*  Ctrl-P ? */
  1549.          printing=1;
  1550.         if ( c == 3 ) /*  Ctrl-C ? */
  1551.          asking=1;
  1552.      }
  1553.      cleardevice();        /* Clear the screen     */
  1554.    }
  1555. }
  1556.  
  1557. /*                         */
  1558. /* MAINWINDOW: Establish the main window for the demo and set  */
  1559. /* a viewport for the demo code.             */
  1560. /*                         */
  1561.  
  1562. void MainWindow( char *header )
  1563. {
  1564.   int height;
  1565.  
  1566.   cleardevice();        /* Clear graphics screen   */
  1567.   setcolor( MaxColors - 1 );     /* Set current color to white */
  1568.   setviewport( 0, 0, MaxX, MaxY, 1 );  /* Open port to full screen   */
  1569.  
  1570.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  1571.   height = textheight( "H" );           /* Get basic text height        */
  1572.  
  1573.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1574.   outtextxy( MaxX/2, 2, header );
  1575.   setviewport( 0, height+4, MaxX, MaxY-(height+4), 1 );
  1576.   DrawBorder();
  1577.   setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1 );
  1578.  
  1579. }
  1580.  
  1581. /*                         */
  1582. /* STATUSLINE: Display a status line at the bottom of the screen. */
  1583. /*                         */
  1584.  
  1585. void StatusLine( char *msg )
  1586. {
  1587.   int height;
  1588.  
  1589.   setviewport( 0, 0, MaxX, MaxY, 1 );  /* Open port to full screen   */
  1590.   setcolor( MaxColors - 1 );     /* Set current color to white */
  1591.  
  1592.   changetextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );
  1593.   settextjustify( CENTER_TEXT, TOP_TEXT );
  1594.   setlinestyle( SOLID_LINE, 0, NORM_WIDTH );
  1595.   setfillstyle( EMPTY_FILL, 0 );
  1596.  
  1597.   height = textheight( "H" );           /* Detemine current height      */
  1598.   bar( 0, MaxY-(height+4), MaxX, MaxY );
  1599.   rectangle( 0, MaxY-(height+4), MaxX, MaxY );
  1600.   outtextxy( MaxX/2, MaxY-(height+2), msg );
  1601.   setviewport( 1, height+5, MaxX-1, MaxY-(height+5), 1 );
  1602.  
  1603. }
  1604.  
  1605. /*                         */
  1606. /* DRAWBORDER: Draw a solid single line around the current  */
  1607. /* viewport.                     */
  1608. /*                         */
  1609.  
  1610. void DrawBorder(void)
  1611. {
  1612.   struct viewporttype vp;
  1613.  
  1614.   setcolor( MaxColors - 1 );     /* Set current color to white */
  1615.  
  1616.   setlinestyle( SOLID_LINE, 0, NORM_WIDTH );
  1617.  
  1618.   getviewsettings( &vp );
  1619.   rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top );
  1620.  
  1621. }
  1622.  
  1623. /*                         */
  1624. /* CHANGETEXTSTYLE: similar to settextstyle, but checks for */
  1625. /* errors that might occur whil loading the font file.      */
  1626. /*                         */
  1627.  
  1628. void changetextstyle(int font, int direction, int charsize)
  1629. {
  1630.   int m;
  1631.   int x,y;
  1632.  
  1633.   graphresult();        /* clear error code     */
  1634.   if ( printing && font == DEFAULT_FONT )
  1635.   {
  1636.       PRT_Resolution ( &x,&y );
  1637.       m = min ( y+60/120, MaxX/600+1 );
  1638.       if (m>1) charsize *=m;
  1639.   }
  1640.   settextstyle(font, direction, charsize);
  1641.   grErrorTest();
  1642. }
  1643.  
  1644. /*                         */
  1645. /* GPRINTF: Used like PRINTF except the output is sent to the  */
  1646. /* screen in graphics mode at the specified co-ordinate.    */
  1647. /*                         */
  1648.  
  1649. int gprintf( int *xloc, int *yloc, char *fmt, ... )
  1650. {
  1651.   va_list  argptr;         /* Argument list pointer   */
  1652.   char str[140];        /* Buffer to build sting into */
  1653.   int cnt;           /* Result of SPRINTF for return */
  1654.  
  1655.   va_start( argptr, fmt );    /* Initialize va_ functions   */
  1656.  
  1657.   cnt = vsprintf( str, fmt, argptr );  /* prints string to buffer */
  1658.   outtextxy( *xloc, *yloc, str );   /* Send string in graphics mode */
  1659.   *yloc += textheight( "H" ) + 2;       /* Advance to next line         */
  1660.  
  1661.   va_end( argptr );        /* Close va_ functions     */
  1662.  
  1663.   return( cnt );        /* Return the conversion count   */
  1664.  
  1665. }
  1666.